#pragma once 
#include <iostream>
#include <sys/ipc.h>
#include <sys/types.h>
#include <sys/shm.h>
#include <string>
#include <cerrno>
#include "log.hpp"
#include <cstring>

using namespace std;

Log log;
const int size = 4096;
const string pathname = "/home/fry";
const int proj_id = 0x6666;

key_t GetKey()
{
	key_t k = ftok(pathname.c_str(), proj_id);
	if (k < 0)
	{
		log(Fatal, "ftok error: %s", strerror(errno));
		exit(1);
	}
	log(Info, "ftok success, key: 0x%x", k);
	return k;
}

int GetSharedMemHelper(int flag)
{
	key_t k = GetKey();
	int shmid = shmget(k, size, flag);
	if (shmid < 0)
	{
		log(Fatal, "create share memory error: %s", strerror(errno));
		exit(2);
	}
	log(Info, "create share memory success, shmid: %d", shmid);
	return shmid;
}

int CreateShm()
{
	return GetSharedMemHelper(IPC_CREAT | IPC_EXCL | 0666);
}

int GetShm()
{
	return GetSharedMemHelper(IPC_CREAT);
}
